package org.csu.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.csu.bbs.entity.Admin;
import org.csu.bbs.entity.Posts;
import org.csu.bbs.entity.Signon;
import org.csu.bbs.entity.UserInfo;
import org.csu.bbs.persistence.AdminMapper;
import org.csu.bbs.persistence.SignonMapper;
import org.csu.bbs.persistence.UserInfoMapper;
import org.csu.bbs.result.Result;
import org.csu.bbs.result.User;
import org.csu.bbs.service.UserService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    AdminMapper adminmapper;
    @Autowired
    SignonMapper signonMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public Admin GetByUsernameAndPassword(String username, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("adminname",username);
        map.put("password",password);
        queryWrapper.allEq(map);
        return adminmapper.selectOne(queryWrapper);
    }
    @Override
    public int DeleteOne(String userid) {
        return signonMapper.delete(new QueryWrapper<Signon>().eq("userid",userid));
    }
    @Override
    public User GetUserByUsernameAndPassword(String username, String password) {
        User user = new User();
        QueryWrapper<Signon> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map=new HashMap<>();
        map.put("userid",username);
        map.put("password",password);
        queryWrapper.allEq(map);
        Signon signon = signonMapper.selectOne(queryWrapper);
        if(signon==null){
            return null;
        }else {
            UserInfo userInfo = userInfoMapper.selectById(username);
//            user.setId(signon.getId());
            user.setUserid(signon.getUserid());
            user.setPassword(signon.getPassword());
            user.setAuthority(userInfo.getAuthority());
            user.setBan(userInfo.getBan());
            user.setEmail(userInfo.getEmail());
            user.setPhone(userInfo.getPhone());
            user.setSex(userInfo.getSex());
            user.setHeaderSculpture(userInfo.getHeaderSculpture());
            return user;
        }
    }
    @Override
    public User updateUser(User user){
        Signon signon = new Signon();
//        signon.setId(user.getId());
        signon.setUserid(user.getUserid());
        signon.setPassword(user.getPassword());
        signonMapper.updateById(signon);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserid(user.getUserid());
        userInfo.setAuthority(user.getAuthority());
        userInfo.setBan(user.getBan());
        userInfo.setEmail(user.getEmail());
        userInfo.setPhone(user.getPhone());
        userInfo.setSex(user.getSex());
        userInfo.setHeaderSculpture(user.getHeaderSculpture());
        userInfoMapper.updateById(userInfo);
        return user;
    }
    @Override
    public List<UserInfo> search(String userid) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(userid!= null, "userid", userid);
        return userInfoMapper.selectList(queryWrapper);
    }
    @Override
    public List<UserInfo> searchAll(){
        return userInfoMapper.selectList(null);
    }
    @Override
    public List<UserInfo> changeBan(UserInfo userInfo){
        userInfoMapper.updateById(userInfo);
        return  userInfoMapper.selectList(null);
    }
    @Override
    public List<UserInfo> changeAuthority(UserInfo userInfo){
        userInfoMapper.updateById(userInfo);
        return  userInfoMapper.selectList(null);
    }
    @Override
    public User GetUserById(String id){
        User user = new User();
        QueryWrapper<Signon> signonQueryWrapper = new QueryWrapper<>();
        signonQueryWrapper.eq("userid", id);
        Signon signon = signonMapper.selectOne(signonQueryWrapper);
        if(signon==null){
            return null;
        }else {
            QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
            userInfoQueryWrapper.eq("userid", id);
            UserInfo userInfo = userInfoMapper. selectOne(userInfoQueryWrapper);
            System.out.println(userInfo);
//            user.setId(signon.getId());
            user.setUserid(signon.getUserid());
            user.setPassword(signon.getPassword());
            user.setAuthority(userInfo.getAuthority());
            user.setBan(userInfo.getBan());
            user.setEmail(userInfo.getEmail());
            user.setPhone(userInfo.getPhone());
            user.setSex(userInfo.getSex());
            user.setHeaderSculpture(userInfo.getHeaderSculpture());
            return user;
        }
    }

    @Override
    public Result changeAvatar(User user) {
        String userid = user.getUserid();
        String avatarUrl = user.getHeaderSculpture();
        try {
            userInfoMapper.changeAvatar(avatarUrl,userid);
            return new Result(200);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(400);
        }
    }
}
